﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Managers.IManagers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace GameEngine.Managers.ConcreteManagers
{
    public class InputManager : GameComponent, IInputManager
    {
        public PlayerIndex playerIndex { get; private set; }
        public GamePadState gamePadState { get; private set; }
        public Keys[] keyBoardState { get; private set; }
        public MouseState mouseState { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="InputManager" /> class.
        /// </summary>
        /// <param name="game">The game.</param>
        public InputManager(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Called when the GameComponent needs to be updated. Override this method with component-specific update code.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to Update</param>
        public override void Update(GameTime gameTime)
        {
            SetPlayerIndex();
            UpdateStates();
        }

        /// <summary>
        /// Updates all the diffrent states.
        /// </summary>
        private void UpdateStates()
        {
            gamePadState = GamePad.GetState(playerIndex);
            keyBoardState = Keyboard.GetState().GetPressedKeys();
            mouseState = Mouse.GetState();
        }

        /// <summary>
        /// Sets the index of the player.
        /// </summary>
        private void SetPlayerIndex()
        {
            playerIndex = PlayerIndex.One;
            for (PlayerIndex index = PlayerIndex.One; index <= PlayerIndex.Four; index++)
            {
                if (GamePad.GetState(index).Buttons.Start == ButtonState.Pressed)
                {
                    playerIndex = index;
                    break;
                }
            }
        }

        private void DetectDisconnectedController()
        { }

        /// <summary>
        /// Determines whether [is backward pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is backward pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsBackwardPressed()
        {
#if XBOX360
            if (gamePadState.DPad.Left == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Left))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is forward pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is forward pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsForwardPressed()
        {
#if XBOX360
            if (gamePadState.DPad.Right == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Right))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is down pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is down pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsDownPressed()
        {
#if XBOX360
            if (gamePadState.DPad.Down == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Down))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is up pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is up pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsUpPressed()
        {
#if XBOX360
            if (gamePadState.DPad.Up == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Up))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is A pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is A pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAPressed()
        {
#if XBOX360
            if (gamePadState.Buttons.A == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Space))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is back pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is back pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsBackPressed()
        {
#if XBOX360
            if (gamePadState.Buttons.B == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Back))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is start pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is start pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsStartPressed()
        {
#if XBOX360
            if (gamePadState.Buttons.Start == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Escape))
                    return true;
            }
#endif
            return false;
        }

        /// <summary>
        /// Determines whether [is special action1 pressed].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is special action1 pressed]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSpecialAction1Pressed()
        {
#if XBOX360
            if (gamePadState.Buttons.Y == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.J))
                    return true;
            }
#endif
            return false;
        }
        

        public bool IsSelectMenuEntry()
        {
#if XBOX360
            if (gamePadState.Buttons.A == ButtonState.Pressed)
            {
                return true;
            }
#else
            for (int i = 0; i < keyBoardState.Length; i++)
            {
                if (keyBoardState[i].Equals(Keys.Enter))
                    return true;
            }
#endif
            return false;
        }
    }
}
